package com.controller;

import com.entity.Blog;
import com.entity.Collection;
import com.google.gson.Gson;
import com.service.impl.BlogServiceImpl;
import com.service.impl.CollectionServiceImpl;
import com.service.intf.BlogService;
import com.service.intf.CollectionService;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionServlet extends BaseServlet {
    private CollectionService collectionService = new CollectionServiceImpl();
    BlogService blogService = new BlogServiceImpl();

    protected void queryCollection(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int userId = WebUtils.parseInt(req.getParameter("userId"), 0);
        List<Collection> collections = collectionService.queryCollectionBlogIdByUserId(userId);
        List<Blog> blogs = new ArrayList<Blog>();
        for (Collection collection : collections) {
            Integer blogId = collection.getBlogId();
            blogs.add(blogService.querySingleBlogByBlogId(blogId));
            System.out.println(blogs);

        }
        req.getSession().setAttribute("blogs", blogs);
        System.out.println(blogs);
        req.getRequestDispatcher("/pages/collection/collection.jsp").forward(req, resp);
    }

    protected void saveCollection(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        Integer userId= blogService.queryUserIdByUserName(username);
        Integer blogId = (Integer) req.getSession().getAttribute("blogId");
        collectionService.saveCollection(blogId,userId);

        resp.sendRedirect(req.getContextPath()+"/user/collectionServlet?action=isCollection&username="+username);
    }

    protected void deleteCollection(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        Integer userId= blogService.queryUserIdByUserName(username);
        Integer blogId = (Integer) req.getSession().getAttribute("blogId");
        collectionService.deleteCollection(blogId,userId);

        resp.sendRedirect(req.getContextPath()+"/user/collectionServlet?action=isCollection&username="+username);
    }


    protected void isCollection(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
////        System.out.println(username+"???");
        Integer userId= blogService.queryUserIdByUserName(username);
//        int userId = WebUtils.parseInt(req.getParameter("userId"), 0);
        Integer blogId = (Integer) req.getSession().getAttribute("blogId");
        boolean collection = collectionService.isCollection(blogId, userId);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("collection",collection);

        Gson gson=new Gson();
        String json = gson.toJson(resultMap);

        resp.getWriter().write(json);
    }

    protected void queryCollectionNumByBlogId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String username = req.getParameter("username");
//        Integer userId= blogService.queryUserIdByUserName(username);
//        List<Collection> collections = collectionService.queryCollectionBlogIdByUserId(userId);
//        req.getSession().setAttribute("collections",collections);
//        req.getSession().setAttribute("collectionNum",collections.size());

        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
        Integer collectionNum = collectionService.queryBlogCollectionNum(blogId);
        req.getSession().setAttribute("collectionNum",collectionNum);

//        req.getRequestDispatcher("/pages/blog/blog_detail.jsp").forward(req, resp);
//        然后去commentServlet查询这篇博客具体的评论
        resp.sendRedirect(req.getContextPath()+"/user/commentServlet?action=queryCommentByBlogId&blogId="+blogId);


    }
}